{extend name="adminheader:header"/}
{block name="content"}
<style type="text/css">
    thead tr th{
        text-align: center;
    }
    tbody tr td{
        text-align: center;
    }
</style>
<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
        <div class="row">
            <div class="col-sm-12">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>商品列表</h5>
                    </div>
                    <div class="ibox-content">
                        <form action="{:url('/admin/Goods/index')}" method="post" role="form" class="form-inline" style="padding-bottom:30px;">
                            商品分类名称:
                            <select class="form-control" name="cid">
                                <option value="">全部</option>
                                {if condition="$category neq null"}
                                    {volist name="category" id="vo"}
                                        <option value="{$vo.id}" {$vo['pid']==0?'disabled':''} {$vo['id']==$cid ?'selected':''}>{$vo.name}</option>
                                    {/volist}
                                {else/}
                                    <option value="" disabled="disabled">暂时无商品分类</option>
                                {/if}
                            </select>&nbsp;&nbsp;
                            商品名称: <input type="text" name="g_name" value="{$g_name|default=''}" placeholder="请输入商品名称" style="width:150px;" class="form-control" />&nbsp;&nbsp;
                            展示地方:
                            <select class="form-control" name="g_show">
                                <option value="">全部</option>
                                <option value="2" {if condition="$g_show eq 1"}selected{/if}>普通商品</option>
                                <option value="3" {if condition="$g_show eq 2"}selected{/if}>官方活动</option>
                            </select>&nbsp;&nbsp;
                            状态:
                            <select class="form-control" name="g_status">
                                <option value="">全部</option>
                                <option value="1" {if condition="$g_status eq 1"}selected{/if}>新添加</option>
                                <option value="2" {if condition="$g_status eq 2"}selected{/if}>上架</option>
                                <option value="3" {if condition="$g_status eq 3"}selected{/if}>下架</option>
                            </select>&nbsp;&nbsp;
                            <input type="submit" class="btn btn-success" value="搜索" style="margin-bottom: 0;"/>
                            <a class="btn btn-danger" href="{:url('/admin/Goods/index')}">清空</a>
                        </form>
                        <div class="table-responsive">
                            <table class="table table-striped table-hover" id="f3">
                                <thead>
                                    <tr>
                                        <th>选项框</th>
                                        <th>序号</th>
                                        <th>商品分类</th>
                                        <th>商品名称</th>
                                        <th>当前价</th>
                                        <th>原价</th>
                                        <th>展示地方</th>
                                        <th>状态</th>
                                        <th>添加时间</th>
                                        <th>操作</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    {if condition="$list neq null"}
                                        {volist name="list" id="vo"}
                                            <tr>
                                                <td><input type="checkbox" data-id="{$vo['id']}" style="cursor: pointer;"></td>
                                                <td>{$vo.id}</td>
                                                <td>{$vo.name}</td>
                                                <td>{$vo.g_name}</td>
                                                <td>￥{$vo.g_cprice}</td>
                                                <td>￥{$vo.g_oprice}</td>
                                                {if condition="$vo.g_show eq 1"/}
                                                    <td>普通产品</td>
                                                {elseif condition="$vo.g_show eq 2"/}
                                                    <td style="color: #0000FF;">官方活动</td>
                                                {/if}
                                                {if condition="$vo.g_status eq 1"}
                                                    <td>新添加</td>
                                                {elseif condition="$vo.g_status eq 2"/}
                                                    <td>上架</td>
                                                {elseif condition="$vo.g_status eq 3"/}
                                                    <td>下架</td>
                                                {/if}
                                                <td>{:date('Y-m-d H:i',$vo['addtime'])}</td>
                                                <td>
                                                    <a class="btn btn-success infos" infoid="{$vo.id}" goodsname="{$vo.g_name}">商品规格</a>
                                                    <a href="{:url('/admin/Goods/edit',array('id'=>$vo['id']))}" class="btn btn-primary">编辑</a>
                                                    <a href="{:url('/admin/Goods/delete',array('id'=>$vo['id']))}" class="btn btn-danger js-ajax-delete">删除</a>
                                                </td>
                                            </tr>
                                        {/volist}
                                    {else/}
                                        <tr>
                                            <td style="font-size:20px;text-align:center">暂无数据</td>
                                        </tr>
                                    {/if}
                                    <tr>
                                        <td colspan="7" style="text-align: left;">
                                            <button onclick="return selectall(true);" class="btn btn-primary">全选</button>
                                            <button onclick="return selectall(false);" class="btn btn-primary">反选</button>
                                            <button onclick="return racking();" class="btn btn-success">上架</button>
                                            <button onclick="return rack();" class="btn btn-info">下架</button>
                                        </td>
                                    </tr>
                                </tbody>
                            </table>
                            <div class="text-left pages">
                                {$page}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
<script src="/admin/js/admin.js"></script>
<script type="text/javascript">

    // 选择
    function selectall(sign){
        var testForm = document.getElementById('f3');
        var ipts = testForm.getElementsByTagName('input');

        if(sign){
            for(var i = 0; i < ipts.length; i++){
                ipts[i].checked = sign;
            }
        }else{
            for(var i = 0; i < ipts.length; i++){
                ipts[i].checked = !ipts[i].checked;
            }
        }
        return false;
    }

    // 上架
    function racking(){
        var testForm = document.getElementById('f3');
        var ipts = testForm.getElementsByTagName('input');
        var ids = '';
        for (var i = 0; i < ipts.length; i++) {
            if(ipts[i].checked){
                ids += $(ipts[i]).attr('data-id')+',';                                      
            }
            // console.log(2);
        }
        $.post("/admin/Goods/racking",{id:ids},function(data){
            if(data){

                location=location;
            }else{
                layer.alert('操作失败');
            }
        });
        // console.log(ids);
    }
    // 下架
    function rack(){
        var testForm = document.getElementById('f3');
        var ipts = testForm.getElementsByTagName('input');
        var ids = '';
        for (var i = 0; i < ipts.length; i++) {
            if(ipts[i].checked){
                ids += $(ipts[i]).attr('data-id')+',';                                  
            }
        };

        $.post("/admin/Goods/rack",{id:ids},function(data){
            if(data){
                
                location=location;
            }else{
                layer.alert('操作失败');
            }
        });
    }

    /* 查看商品的规格 */
    $('.infos').click(function(){
        var id = $(this).attr('infoid');
        var gname = $(this).attr('goodsname');
        openIframeLayer("{:url('/admin/goods/info')}?id=" + id, gname+'的商品规格', {
            area: ['800px', '400px'],
            yes: function (index, layero) {
                layer.close(index); //如果设定了yes回调，需进行手工关闭
            }
        });
    });
</script>
{/block}